package com.tb.ClassSchedule;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by guich on 2016/8/21.
 */
public class LessonViewActivity extends AppCompatActivity implements View.OnClickListener{

    public static LessonViewActivity main;
    private Lesson lesson;

    private TextView tvName;
    private TextView tvTeacher;
    private TextView tvAddress;
    private TextView tvWeek;
    private TextView tvSection;
    private View delete;

    private int color;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson_detail);
        main = this;

        Bundle bundle = getIntent().getExtras();
        color = getIntent().getIntExtra("Color",0);
        this.lesson = (Lesson) bundle.getSerializable("LessonInfo");
        Log.d("传值测试",lesson.getName() + lesson.getAddress());

        initTitle();

        tvName = (TextView) findViewById(R.id.detail_name);
        tvTeacher = (TextView) findViewById(R.id.detail_teacher);
        tvAddress = (TextView) findViewById(R.id.detail_address);
        tvWeek = (TextView) findViewById(R.id.detail_week);
        tvSection = (TextView) findViewById(R.id.detail_section);
        delete = findViewById(R.id.detail_delete);
        delete.setOnClickListener(this);

        findViewById(R.id.bg).setBackgroundColor(color);
        initInfo(lesson);
    }

    public void initInfo(Lesson lesson) {
        tvName.setText(lesson.getName());
        tvTeacher.setText(lesson.getTeacher());
        tvAddress.setText(lesson.getAddress());

        String weekStr = "";
        if(lesson.getEndWeek() == lesson.getStartWeek()){
            weekStr = "第 " + lesson.getStartWeek() + " 周";
        }else{
            weekStr = "第 " + lesson.getStartWeek() + " 周 ~ 第 " + lesson.getEndWeek() + " 周";
        }
        tvWeek.setText(weekStr);

        String[] weekday = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        String sectionStr = "";
        if(lesson.getStartSection() == lesson.getEndSection()){
            sectionStr = weekday[lesson.getWeekday() - 1] + " : 第 " + lesson.getStartSection() + " 节";
        }else{
            sectionStr = weekday[lesson.getWeekday() - 1] + " : 第 " + lesson.getStartSection() + " 节 ~ 第 " + lesson.getEndSection() + " 节";
        }
        tvSection.setText(sectionStr);

        this.lesson = lesson;
    }

    private void initTitle(){
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("课程详细信息");
        setSupportActionBar(toolbar);
        //显示返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            toolbar.setPadding(0,MainActivity.statusBarHeight,0,0);
        }
        toolbar.setBackgroundColor(MainActivity.sharedPreferences.getInt("color",MainActivity.DEFAULT_THEME));
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if(item.getItemId() == R.id.action_edit){
                    editAction();
                }
                return true;
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            //处理返回按钮
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_edit,menu);
        return true;
    }





    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.detail_delete){
            deleteAction();
        }
    }

    private void editAction(){
        Bundle bundle = new Bundle();
        bundle.putSerializable("LessonInfo",lesson);
        Intent intent = new Intent(this,EditClassActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    private void deleteAction(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认提示");
        builder.setMessage("\n确认删除本课所有相关信息？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                saveToDatabase();
                Toast.makeText(main,"删除成功！",Toast.LENGTH_SHORT).show();
                MainActivity.main.draw();
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        builder.show();
    }

    private void saveToDatabase(){
        LessonDatabaseHelper dbHelper = new LessonDatabaseHelper(MainActivity.main, "Lessons.db", null, 1);
        SQLiteDatabase       db       = dbHelper.getWritableDatabase();
        db.delete("Lesson","ClassName = ? and " +
                "WeekDay = ? and " +
                "StartSection = ? and " +
                "EndSection = ? and " +
                "StartWeek = ? and " +
                "EndWeek = ? and " +
                "TeacherName = ? and " +
                "Address = ?",new String[]{
                lesson.getName(),
                "" + lesson.getWeekday(),
                "" + lesson.getStartSection(),
                "" + lesson.getEndSection(),
                "" + lesson.getStartWeek(),
                "" + lesson.getEndWeek(),
                lesson.getTeacher(),
                lesson.getAddress()});
    }


}
